//
//  TGDataTool.m
//  GroupBuy
//
//  Created by wwj on 15/11/3.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGDataTool.h"
#import "TGCityModel.h"
#import "MJExtension.h"
#import "TGCitySectionModel.h"
#import "TGCategoryModel.h"
#import "TGOrderModel.h"

#define kFilePath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"visitedCityNames.data"]

@interface TGDataTool ()
/** 访问过的城市名称 */
@property (strong, nonatomic) NSMutableArray *visitedCityNames;
/** 访问过的城市组 */
@property (strong, nonatomic) TGCitySectionModel *visitedCitySection;

@end

@implementation TGDataTool

+ (instancetype)shareDataTool{
    static TGDataTool *dataTool;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataTool = [[TGDataTool alloc] init];
    });
    return dataTool;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        //添加城市数据
        [self addCityData];
        //添加分类数据
        [self addCategoryData];
        //添加排序
        [self addOrderData];
    }
    return self;
}

-(void)setCurrentCity:(TGCityModel *)currentCity{
    if (currentCity == nil) {
        return;
    }
    _currentCity = currentCity;
    _currentDistrict = kAllDistrictStr;
    [_visitedCityNames removeObject:currentCity.name];
    [_visitedCityNames insertObject:currentCity.name atIndex:0];
    
    [_visitedCitySection.cities removeObject:currentCity];
    [_visitedCitySection.cities insertObject:currentCity atIndex:0];
    
    if (![_totalCityGrouyArray containsObject:_visitedCitySection]) {
        NSMutableArray *array = (NSMutableArray *)_totalCityGrouyArray;
        [array insertObject:_visitedCitySection atIndex:0];
    }
    
    [NSKeyedArchiver archiveRootObject:_visitedCityNames toFile:kFilePath];
    [[NSNotificationCenter defaultCenter] postNotificationName:kCityChange object:nil];
}

-(void)setCurrentCategory:(NSString *)currentCategory{
    _currentCategory = currentCategory;
    [[NSNotificationCenter defaultCenter] postNotificationName:kCategoryChange object:nil];
}

-(void)setCurrentDistrict:(NSString *)currentDistrict{
    _currentDistrict = currentDistrict;
    [[NSNotificationCenter defaultCenter] postNotificationName:kDistrictChange object:nil];
}

-(void)setCurrentOrder:(TGOrderModel *)currentOrder{
    _currentOrder = currentOrder;
    [[NSNotificationCenter defaultCenter] postNotificationName:kOrderChange object:nil];
}

-(TGOrderModel *)getOrderWithName:(NSString *)name{
    for (TGOrderModel *order in _orderArray) {
        if ([name isEqualToString:order.name]) {
            return order;
        }
    }
    return nil;
}

-(TGCityModel *)getCityWithName:(NSString *)name{
    return [_dictCitySection objectForKey:name];
}

#pragma mark 通过分类名称取得图标
- (NSString *)iconWithCategoryName:(NSString *)name{
    for (TGCategoryModel *c in _categoryArray) {
        // 1.分类名称一致
        if ([c.name isEqualToString:name]) return c.icon;
        
        // 2.有这个子分类
        if ([c.subcategories containsObject:name]) return c.icon;
    }
    
    return nil;
}

#pragma -mark 添加城市数据
- (void)addCityData{
    NSMutableArray *tempArray = [NSMutableArray array];
    //添加热门城市
    TGCitySectionModel *hotSection = [[TGCitySectionModel alloc] init];
    hotSection.name = @"热门城市";
    hotSection.cities = [NSMutableArray array];
    [tempArray addObject:hotSection];
    
    NSArray *azArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Cities.plist" ofType:nil]];
    
    _dictCitySection = [NSMutableDictionary dictionary];
    for (NSDictionary *dict in azArray) {
        TGCitySectionModel *secion = [TGCitySectionModel mj_objectWithKeyValues:dict];
        [tempArray addObject:secion];
        for (TGCityModel *city in secion.cities) {
            if (city.isHot) {
                [hotSection.cities addObject:city];
            }
            [_dictCitySection setObject:city forKey:city.name];
        }
    }
    
    //添加最新访问城市
    _visitedCityNames = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath];
    if (_visitedCityNames == nil) {
        _visitedCityNames = [NSMutableArray array];
    }
    
    TGCitySectionModel *visitySection = [[TGCitySectionModel alloc] init];
    visitySection.name = @"最近访问";
    visitySection.cities = [NSMutableArray array];
    
    for (NSString *name in _visitedCityNames) {
        TGCityModel *city = _dictCitySection[name];
        [visitySection.cities addObject:city];
    }
    _visitedCitySection = visitySection;
    if (_visitedCityNames.count) {
        [tempArray insertObject:visitySection atIndex:0];
    }
    
    _totalCityGrouyArray = tempArray;
}

#pragma -mark 添加分类数据
-(void)addCategoryData{
    NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Categories.plist" ofType:nil]];
    NSMutableArray *array = [NSMutableArray array];
    TGCategoryModel *category = [[TGCategoryModel alloc] init];
    category.name = @"全部分类";
    category.icon = @"ic_filter_category_-1.png";
    [array addObject:category];
    for (NSDictionary *dict in dictArray) {
        TGCategoryModel *model = [TGCategoryModel mj_objectWithKeyValues:dict];
        [array addObject:model];
    }
    _categoryArray = array;
}

#pragma -mark 添加排序
-(void)addOrderData{
    NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Orders.plist" ofType:nil]];
    NSMutableArray *array = [NSMutableArray array];
    for (int index = 0; index < dictArray.count; index++) {
        TGOrderModel *model = [[TGOrderModel alloc] init];
        model.name = dictArray[index];
        model.index = index + 1;
        [array addObject:model];
    }
    _orderArray = array;
}
@end
